package com.zkt.subject.application.controller;

import cn.hutool.core.util.ObjUtil;
import com.alibaba.fastjson2.JSON;
import com.zkt.subject.application.convert.SubjectCategoryDTOConverter;
import com.zkt.subject.application.convert.SubjectLabelDTOConverter;
import com.zkt.subject.application.convert.SubjectUserLabelDTOConverter;
import com.zkt.subject.application.dto.SubjectCategoryDTO;
import com.zkt.subject.entity.SubjectLabelDTO;
import com.zkt.subject.application.dto.SubjectUserLabelDTO;
import com.zkt.subject.common.entity.Result;
import com.zkt.subject.common.util.LoginUtil;
import com.zkt.subject.domain.entity.SubjectCategoryBO;
import com.zkt.subject.domain.entity.SubjectUserLabelBO;
import com.zkt.subject.domain.service.SubjectUserLabelDomainService;
import com.zkt.subject.infra.entity.UserInfo;
import com.zkt.subject.infra.rpc.UserRPC;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/27
 * @description 用户标签关系controller层
 **/

@RestController
@RequestMapping("/userLabel")
@Slf4j
public class SubjectUserLabelController {
	
	@Resource
	private SubjectUserLabelDomainService subjectUserLabelDomainService;
	
	@Resource
	private UserRPC userRPC;
	
	/**
	 * 添加用户标签关系
	 *
	 * @param subjectUserLabelDTO 用户标签关系
	 * @return 是否成功
	 */
	@PostMapping("add")
	public Result<Boolean> add(@RequestBody SubjectUserLabelDTO subjectUserLabelDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectUserLabelController.add.dto:{}", JSON.toJSONString(subjectUserLabelDTO));
			}
			
			if (ObjUtil.isEmpty(subjectUserLabelDTO.getUserId())) {
				UserInfo userInfo = userRPC.getUserInfo(LoginUtil.getLoginId());
				if (ObjUtil.isEmpty(userInfo) || ObjUtil.isEmpty(userInfo.getId())) {
					throw new Exception("用户id不能为空");
				}
				
				subjectUserLabelDTO.setUserId(userInfo.getId());
			}
			if (ObjUtil.isEmpty(subjectUserLabelDTO.getLabelIds())) {
				throw new Exception("标签id不能为空");
			}
			
			SubjectUserLabelBO subjectUserLabelBO = SubjectUserLabelDTOConverter.INSTANCE.convertDtoToBo(subjectUserLabelDTO);
			
			return Result.ok(subjectUserLabelDomainService.add(subjectUserLabelBO));
		} catch (Exception e) {
			log.error("SubjectUserLabelController.add.error:{}", e.getMessage(), e);
			return Result.fail("添加用户标签关系失败");
		}
	}
	
	/**
	 * 删除用户标签关系
	 *
	 * @param subjectUserLabelDTO 需要删除的用户标签关系
	 * @return 是否成功
	 */
	@PostMapping("remove")
	public Result<Boolean> remove(@RequestBody SubjectUserLabelDTO subjectUserLabelDTO) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectUserLabelController.remove.dto:{}", JSON.toJSONString(subjectUserLabelDTO));
			}
			
			if (ObjUtil.isEmpty(subjectUserLabelDTO.getUserId())) {
				UserInfo userInfo = userRPC.getUserInfo(LoginUtil.getLoginId());
				if (ObjUtil.isEmpty(userInfo) || ObjUtil.isEmpty(userInfo.getId())) {
					throw new Exception("用户id不能为空");
				}
				
				subjectUserLabelDTO.setUserId(userInfo.getId());
			}
			if (ObjUtil.isEmpty(subjectUserLabelDTO.getLabelIds())) {
				throw new Exception("标签id不能为空");
			}
			
			SubjectUserLabelBO subjectUserLabelBO = SubjectUserLabelDTOConverter.INSTANCE.convertDtoToBo(subjectUserLabelDTO);
			
			return Result.ok(subjectUserLabelDomainService.remove(subjectUserLabelBO));
		} catch (Exception e) {
			log.error("SubjectUserLabelController.remove.error:{}", e.getMessage(), e);
			return Result.fail("删除用户标签关系失败");
		}
	}
	
	/**
	 * 获取用户期望标签列表
	 *
	 * @param username 用户名/账号
	 * @return 用户信息
	 */
	@GetMapping("getUserLabels")
	public Result<List<SubjectLabelDTO>> getUserLabels(@RequestParam(value = "username", required = false) String username) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectUserLabelController.getUserLabels.username:{}", username);
			}
			
			return Result.ok(subjectUserLabelDomainService.getUserLabels(userRPC.getUserInfo(username)));
		} catch (Exception e) {
			log.error("SubjectUserLabelController.getUserLabels.error:{}", e.getMessage(), e);
			return Result.fail("获取用户信息失败");
		}
	}
	
	
	/**
	 * 获取用户期望的分类和标签
	 *
	 * @param username 用户名/账号
	 * @return 用户信息
	 */
	@GetMapping("getUserCategoryAndLabels")
	public Result<List<SubjectCategoryDTO>> getUserCategoryAndLabels(@RequestParam(value = "username", required = false) String username) {
		try {
			if (log.isInfoEnabled()) {
				log.info("SubjectUserLabelController.getUserCategoryAndLabels.username:{}", username);
			}
			
			List<SubjectCategoryBO> subjectCategoryBOList =
					subjectUserLabelDomainService.queryCategoryAndLabel(userRPC.getUserInfo(username));
			
			return Result.ok(dealCategoryAndLabel(subjectCategoryBOList));
		} catch (Exception e) {
			log.error("SubjectUserLabelController.getUserCategoryAndLabels.error:{}", e.getMessage(), e);
			return Result.fail("获取用户信息失败");
		}
	}
	
	private List<SubjectCategoryDTO> dealCategoryAndLabel(List<SubjectCategoryBO> subjectCategoryBOList) {
		List<SubjectCategoryDTO> dtoList = new LinkedList<>();
		subjectCategoryBOList.forEach(bo -> {
			List<SubjectCategoryDTO> dtoCategoryList = new LinkedList<>();
			bo.getCategoryBOList().forEach(boCategory -> {
				SubjectCategoryDTO dtoCategory = SubjectCategoryDTOConverter.INSTANCE.convertBoToCategoryDTO(boCategory);
				List<SubjectLabelDTO> labelDTOList =
						SubjectLabelDTOConverter.INSTANCE.convertListBoToLabelDTO(boCategory.getLabelBOList());
				dtoCategory.setLabelDTOList(labelDTOList);
				dtoCategoryList.add(dtoCategory);
			});
			
			SubjectCategoryDTO dto = SubjectCategoryDTOConverter.INSTANCE.convertBoToCategoryDTO(bo);
			dto.setCategoryDTOList(dtoCategoryList);
			dtoList.add(dto);
		});
		return dtoList;
	}
	
}